package com.atsmart.app.sql;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;

import com.atsmart.app.model.TaskInfo;
import com.atsmart.app.model.TaskListInfo;

public class TaskNumSqlHelper {

	private SQLiteDatabase db = null;

	/**
	 * 打开数据库
	 */
	public void open(SqlHelper dh) {
		// dh = new SqlHelper(null, "atsmart", null,SqlHelper.getDBVersion());
		db = dh.getWritableDatabase();
	}

	public synchronized void addAttribute(int task_id,int tem_id) {
		String strSql = "insert or replace into task_nums (task_id,tem_id) values(?,?);";
		try {
			SQLiteStatement stm = db.compileStatement(strSql);
			stm.bindLong(1, task_id);
			stm.bindLong(2, tem_id);
			stm.execute();
			stm.clearBindings();
			stm.close();
		} catch (Exception e) {
			// will not happen
		}
	}

	public int getTaskList(int task_id) {
		String sql = "select task_id from task_nums where task_id="+task_id;
		Cursor cursor=null;
		int num =0;
		try {
			 cursor = db.rawQuery(sql, null);
			if (cursor != null && cursor.moveToFirst()) {
				num =cursor.getCount();
			}
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		} finally {
			cursor.close();
		}
		return num;
	}
	public List<Integer> getTaskList1(int task_id) {
		String sql = "select tem_id from task_nums where task_id="+task_id;
		Cursor cursor=null;
		List<Integer> l=null;
		try {
			 cursor = db.rawQuery(sql, null);
			if (cursor != null && cursor.moveToFirst()) {
				l =new ArrayList<Integer>();
				for (int i = 0; i < cursor.getCount(); i++) {
					l.add(cursor.getInt(0));
					cursor.moveToNext();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			cursor.close();
		}
		return l;
	}
	public int getTaskListCount(int task_id,int tem_id) {
		String sql = "select task_id from task_nums where task_id="+task_id +" and tem_id="+tem_id;
		Cursor cursor=null;
		int num =0;
		try {
			 cursor = db.rawQuery(sql, null);
			if (cursor != null && cursor.moveToFirst()) {
				num =cursor.getCount();
			}
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		} finally {
			cursor.close();
		}
		return num;
	}
	public void updateTemplate(int task_id,int num) {
		 ContentValues cv = new ContentValues();
		 cv.put("template_num", num);
		 db.update("task_info", cv,  " tid="+task_id, null);
		 }
}
